#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_btn_load_clicked()
{
    _fileName=QFileDialog::getOpenFileName(this,"选择要打开的文件",
                                           QString(),
                                           "Desktop Entries (*.desktop)");
    if(_fileName.isEmpty())
        return;

    loadFile(_fileName);
}

void Dialog::on_pick_exec_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(this,"选择要使用的可执行文件");
    if(fileName.isEmpty())
        return;

    ui->exec->setText(fileName);
    ui->name->setText(QFileInfo(fileName).baseName());
}

void Dialog::on_pick_icon_clicked()
{
    QString fileName=QFileDialog::getOpenFileName(this,"选择要使用的图形文件",
                                                  QString(),
                                                  "Images (*.png *.svg)");
    if(fileName.isEmpty())
        return;

    ui->icon->setText(fileName);
}

void Dialog::on_btn_clean_clicked()
{
    ui->location->clear();
    ui->content->clear();
}

void Dialog::on_pick_path_clicked()
{
    QString path=QFileDialog::getExistingDirectory(this,"选择运行时目录");
    if(path.isEmpty())
        return;

    ui->path->setText(path);
}

void Dialog::on_btn_generate_clicked()
{
    QString exec=ui->exec->text().trimmed();
    if(exec.isEmpty())
        return;

    _appName=ui->name->text().trimmed();
    if(_appName.isEmpty())
        return;

    _tmpFileName=P_TMP + QUuid::createUuid().toString();
    QSettings cfg(_tmpFileName,QSettings::IniFormat);
    cfg.setValue(D_ENTRY + "Type","Application");
    cfg.setValue(D_ENTRY + "Name",_appName);
    cfg.setValue(D_ENTRY + "Exec",exec);
    QString comment=ui->comment->text().trimmed();
    if(!comment.isEmpty())
        cfg.setValue(D_ENTRY + "Comment",comment);

    QString icon=ui->icon->text().trimmed();
    if(!icon.isEmpty())
        cfg.setValue(D_ENTRY + "Icon",icon);

    QString path=ui->path->text().trimmed();
    if(!path.isEmpty())
        cfg.setValue(D_ENTRY + "Path",path);

    QString categories=ui->categories->text().trimmed();
    if(!categories.isEmpty())
        cfg.setValue(D_ENTRY + "Categories",categories);

    cfg.sync();
    loadTmpFile(_tmpFileName);
}

void Dialog::loadTmpFile(const QString &fileName)
{
    if(!QFile::exists(fileName))
        return;

    QFile file(fileName);
    if(file.open(QFile::ReadOnly | QFile::Text))
    {
        QStringList content;
        QTextStream text(&file);
        QString line;
        while (text.readLineInto(&line)) {
            if(!line.startsWith("["))
                content.append(line);
        }

        if(!content.isEmpty())
        {
            ui->content->setPlainText("[Desktop Entry]");
            ui->content->appendPlainText(content.join("\n"));
        }
    }
}

void Dialog::loadFile(const QString &fileName)
{
    if(!QFile::exists(fileName))
    {
        ui->location->setText(fileName + " 不存在");
        return;
    }

    _fileName=fileName;
    ui->location->setText(_fileName);
    QFile file(_fileName);
    if(file.open(QFile::ReadOnly | QFile::Text))
    {
        QTextStream text(&file);
        QString content=text.readAll();
        file.close();
        if(!content.isEmpty())
            ui->content->setPlainText(content);
    }
}

void Dialog::on_btn_write_clicked()
{
    QString content=ui->content->toPlainText();
    if(content.isEmpty())
        return;

    if(!_appName.isEmpty())
    {
        QString path=QFileDialog::getExistingDirectory(this,"选择要保存的位置");
        if(path.isEmpty())
            return;
        _fileName=path + QString("/") + _appName + ".desktop";
    }

    if(_fileName.isEmpty())
    {
        _fileName=QFileDialog::getSaveFileName(this,"选择要保存的文件名");
        if(_fileName.isEmpty())
            return;
        _fileName += ".desktop";
    }

    QFile file(_fileName);
    if(file.open(QFile::WriteOnly | QFile::Text))
    {
        QTextStream text(&file);
        text << content;
        text.flush();
        file.close();
        ui->location->setText(_fileName + " 已保存");
    }
    else
        ui->location->setText(_fileName + " 保存失败，请确定有写入权限");
}

void Dialog::on_btn_clean_setting_clicked()
{
    ui->name->clear();
    ui->exec->clear();
    ui->comment->clear();
    ui->path->clear();
    ui->icon->clear();
    ui->categories->clear();
}
